<?php if(filter_input(INPUT_GET, "success")): ?>
<div class="row">
    <div class="col-md-12">
        <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            Aktifasi berhasil dilakukan.
        </div>
    </div>
    
    <div class="col-md-12">
        <button style="margin: 20px 30px 20px 0;" tabindex="8" class="btn btn-info pull-left" type="button" onclick="location.href = '<?=base_url("network/downline_activate")?>'"><i class="fa fa-recycle"></i> Refresh</button>
    </div>
</div>
<?php else : ?>
    <?php if(isset($error_message) && $error_message != ""): ?>
    <div class="row">
        <div class="col-md-12">
            <div class="alert alert-danger alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <?= $error_message ?>
            </div>
        </div>
    </div>
    <?php endif; ?>

<link href="<?= base_url("assets") ?>/css/select2.css" rel="stylesheet">
<link href="<?= base_url("assets") ?>/css/select2-bootstrap.css" rel="stylesheet">
<link href="<?= base_url("assets") ?>/css/jquery.dataTables.css" rel="stylesheet">

<div class="col-md-12">
    <table id="act-table" class="table table-bordered table-hover" cellspacing="0" width="100%">
        <thead>
            <tr class="success">
                <th>No</th>
                <th>Nama</th>
                <th>Nomor HP</th>
                <th>Email</th>
                <th>Tanggal Registrasi</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            if($list_downline->num_rows() > 0): 
                foreach($list_downline->result() as $key => $row):
            ?>
            <tr>
                <td><?= $key + 1?></td>
                <td><?= _get_raw_object($row, "nama_registran")?></td>
                <td><?= _get_raw_object($row, "no_hp_registran")?></td>
                <td><?= _get_raw_object($row, "email_registran")?></td>
                <td><?= _get_raw_object($row, "date_submit")?></td>
                <td>
                    <button data-row-id="<?= _get_raw_object($row, "id_request")?>" data-row-name="<?= _get_raw_object($row, "nama_registran")?>" class="btn btn-primary" data-toggle="modal" data-target="#act-modal" data-backdrop="static" data-keyboard="false">
                        <i class="fa fa-unlock"></i>
                    </button>
                </td>
            </tr>
            <?php
                endforeach;
            endif;
            ?>
        </tbody>
        <tfoot>
            <tr class="success">
                <th>No</th>
                <th>Nama</th>
                <th>Nomor HP</th>
                <th>Email</th>
                <th>Tanggal Registrasi</th>
                <th>Action</th>
            </tr>
        </tfoot>
    </table>
</div>

<!-- Modal -->
<div class="modal fade" id="act-modal" tabindex="-1" role="dialog" aria-labelledby="act-modal-label" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="act-modal-label">Aktivasi</h4>
            </div>
            <?= form_open("", array("class" => "contact_form", "role" => "form", "id" => "activate-member")) ?>
            <div class="modal-body">
                Pilih PIN untuk melakukan aktifasi:
                <select class="form-control" required id="pin" name="pin">
                    <option value="">---Pilih PIN---</option>
                    <?php 
                    if($list_pin_active->num_rows() > 0): 
                        foreach($list_pin_active->result() as $key => $row):
                    ?>
                    <option value="<?= _get_raw_object($row, "id_request") ?>"><?= _get_raw_object($row, "key_token") ?></option>
                    <?php
                        endforeach;
                    endif;
                    ?>
                </select>
                <br/>
                <div class="alert alert-info" role="alert">
                    PIN dapat dibeli di menu request pin, atau dapat langsung data ke kantor.
                </div>
                <input type="hidden" name="rowid" id="rowid">
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" id="btn-simpan" data-loading-text="Sedang diproses...">Simpan</button>
                <button type="button" class="btn btn-warning" data-dismiss="modal">Batal</button>
            </div>
            <?= form_close() ?>
        </div>
    </div>
</div>

<script src="<?= base_url("assets") ?>/js/select2.min.js"></script>
<script src="<?= base_url("assets") ?>/js/jquery.dataTables.min.js"></script>
<script>
    $(document).ready(function() {
        var oTable = $('#act-table').dataTable();

        $("#pin").select2();

        $('#act-modal').on('show.bs.modal', function(event) {
            var button = $(event.relatedTarget);
            var rowid = button.data('row-id');
            var name = button.data('row-name');

            var modal = $(this);
            modal.find('.modal-title').text('Mengaktifkan downline: ' + name);
            modal.find('.modal-body input[type=hidden]').val(rowid);
        });

        $("#activate-member").on("submit", function() {
            if ($("#pin").is(":valid")) {
                $(this).button("loading");
            } else {
                alert("Masukkan pin yang valid");
                
                return false;
            }
        });
    });
</script>

<?php endif;